package com.vanzo.talkie;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.vanzo.Tpplication;
import com.vanzo.encryption.R;
import com.vanzo.instruction.AT;
import com.vanzo.instruction.Instruction;

/**
 * Created by tisen on 2018/3/15.
 */

public class TestActivity extends AppCompatActivity {


    private Tpplication tpplication;
    private Instruction instruction;

    private TextView version;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tpplication = (Tpplication) getApplication();
        instruction = tpplication.getInstruction();
        instruction.setPower(true);
        setContentView(R.layout.activity_factory_test);
        findViewById(R.id.correct_button).setOnClickListener(resultClickListener);
        findViewById(R.id.error_button).setOnClickListener(resultClickListener);
        version = (TextView) findViewById(R.id.factory_test_version);
        version.setText("对讲模块软件版本号:null");
        instruction.start();
        instruction.setListener(new Instruction.OnResultListener() {
            @Override
            public void onResult(String result) {
                if (result.contains(AT.POWER_STATUS_1)) {
                    sendVersionAsk.sendEmptyMessage(0);
                }
            }
        });
    }

    private Handler sendVersionAsk = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            instruction.getVersion(new Instruction.OnResultListener() {
                @Override
                public void onResult(String result) {
                    if (result.contains(AT.VERSION)) {
                        int index = result.indexOf(AT.VERSION);
                        String version = result.substring(index + AT.DMO_VERSION.length() - 1);
                        Message message = new Message();
                        message.obj = version;
                        uiHandler.sendMessage(message);
                    }
                }
            });
        }
    };
    private Handler uiHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String versionString = (String) msg.obj;
            version.setText("模块软件版本:" + versionString);
        }
    };

    @Override
    protected void onDestroy() {
        instruction.setPower(false);
        super.onDestroy();
    }

    View.OnClickListener resultClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.correct_button:
                    TestActivity.this.setResult(RESULT_OK);
                    finish();
                    break;
                case R.id.error_button:
                    TestActivity.this.setResult(RESULT_CANCELED);
                    finish();
                    break;
            }
        }
    };


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return keyCode == KeyEvent.KEYCODE_BACK || super.onKeyDown(keyCode, event);
    }
}
